package com.yylany.service;

import com.yylany.bo.GoodsData;
import com.yylany.bo.GoodsResult;
import com.yylany.mapper.TbSpuMapper;
import com.yylany.utils.Verification;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * Description: pinyougou
 *
 * @author <a href="mailto:17777502615@163.com">yaoyonglong</a>
 * @date 2018/12/20
 * @since 1.0.0
 */
@Service
public class GoodsService {

    @Autowired
    private TbSpuMapper spuMapper;

    /**
     * 根据条件查询good
     * <p>
     * 之前的代码
     * Example example = new Example(TbSpu.class);
     * Example.Criteria criteria = example.createCriteria();
     * criteria.andEqualTo("cat3Id", result.getCatid());
     * List<TbSpu> tbSpus = spuMapper.selectByExample(example);
     *
     * @param result 传过来的条件
     */
    public List<GoodsData> getGoods(GoodsResult result) {

        // [[1,2],[2,7]]


        // 拼接sql 语句进行 排序
        if (result.getSort_by() != null) {
            result.setSort_by("ORDER BY ts." + result.getSort_by());
        }

        // 进行属性选择
        if (result.getSpec_list() != null) {
            String s = Verification.jsonFormatSQL(result.getSpec_list());
            if (!StringUtils.isNotEmpty(s)) {
                result.setSpec_list(null);
            } else {
                result.setSpec_list(s);
            }

        }

        // 生成 分页条件
        if (result.getPage() != null && result.getPer_page() != null) {
            result.setPage((result.getPage() - 1) * result.getPer_page());
        }

        //TODO 剩下一个 排序没有弄
        List<GoodsData> resultData = spuMapper.findAllByCatId(result);

        return resultData;
    }

    static final String str = null;

    public void test(int id, int num, BigDecimal money) {


        synchronized (str) {

        }


    }

}
